package com.littcore.saap.common.session;

import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

import com.littcore.common.BeanManager;
import com.littcore.saap.common.vo.LoginUserVo;
import com.littcore.saap.core.common.SaapConstants;
import com.littcore.shield.vo.ILoginVo;

import me.chanjar.weixin.common.util.StringUtils;

public class SessionRequestWrap extends HttpServletRequestWrapper{
    
    public static  String cookieSessionName="SessionId";
    
    public static String SESSION_ATTR="Session";
    
   //private SessionCacheManager sessionManager;
   private SessionManager sessionManager;
  // private TokenCacheManager tokenManager=BeanManager.getBean("tokenCacheManager",TokenCacheManager.class);
    public SessionRequestWrap(HttpServletRequest request) {
        super(request);
        sessionManager=BeanManager.getBean("sessionManager",SessionManager.class);
        // TODO Auto-generated constructor stub
    }
    
    public String getRequestedSessionId() {
       if(this.isRequestedSessionIdFromCookie()){
           return this.getSessionIdFromCookie();
       }
       
       if(this.isRequestedSessionIdFromURL()){
           return this.getTokenFromParam();
       }
       return "";
         
    }

    
    /**
     * The default behavior of this method is to return getSession(boolean create)
     * on the wrapped request object.
     */
    public HttpSession getSession(boolean create) {
      if(create){
          HttpSession session = super.getSession();
          if (this.isRequestedSessionIdFromCookie()) {
              ILoginVo currentLoginVo = sessionManager.getSession(this.getSessionIdFromCookie());

              session.setAttribute(SaapConstants.SESSION_USER, currentLoginVo);
          } else if (this.isRequestedSessionIdFromURL()) {
              ILoginVo currentLoginVo = sessionManager.getSession(this.getTokenFromParam());
              session.setAttribute(SaapConstants.SESSION_USER, currentLoginVo);
          }else{
              session.setAttribute(SaapConstants.SESSION_USER, null); 
          }
         // this.setCurrentSession(session);
          return session;
      }else{
          return super.getSession();
      }
    }
    
    /**
     * The default behavior of this method is to return getSession()
     * on the wrapped request object.
     */
    public HttpSession getSession() {
      return this.getSession(true);
    }
    
    private boolean isSessionValid(){
        String sessionId="";
        sessionId = getSessionIdFromCookie();
        if(StringUtils.isNotEmpty(sessionId)){
          return  sessionManager.isContain(sessionId);
        }else {
          String token = getTokenFromParam();
          if(StringUtils.isNotEmpty(token)){
              return sessionManager.isContain(token);
          }
        }
        return false;
    }

    public String getSessionIdFromCookie() {
        String sessionId="";
        Cookie[] cookies= this.getCookies();
        if(cookies==null){
            return "";
        }
        for(Cookie cookie:cookies){
            if(cookie.getName().equalsIgnoreCase(cookieSessionName)){
                sessionId=cookie.getValue();
            }
        }
        return sessionId;
    }

    /**
     * The default behavior of this method is to return isRequestedSessionIdValid()
     * on the wrapped request object.
     */ 

    public boolean isRequestedSessionIdValid() {
  return this.isSessionValid();
    }
     
    
    /**
     * The default behavior of this method is to return isRequestedSessionIdFromCookie()
     * on the wrapped request object.
     */
    public boolean isRequestedSessionIdFromCookie() {
  
        return StringUtils.isNotEmpty(this.getSessionIdFromCookie());
    }
    
        /**
     * The default behavior of this method is to return isRequestedSessionIdFromURL()
     * on the wrapped request object.
     */ 
    public boolean isRequestedSessionIdFromURL() {
       String token = getTokenFromParam();
       return StringUtils.isNotEmpty(token);
    }

       public String getTokenFromParam() {
            String token= this.getParameter("token");
            return token;
        }
    
    /**
     * The default behavior of this method is to return isRequestedSessionIdFromUrl()
     * on the wrapped request object.
     */
    public boolean isRequestedSessionIdFromUrl() {
        String token = getTokenFromParam();
        return StringUtils.isNotEmpty(token);
    }
    
    
/*    public String commitSession(String sessionId,String loginDevice){
       if(StringUtils.isNotEmpty(sessionId)){
           HttpSession httpSession=this.getCurrentSession();
           LoginUserVo currentLoginVo=(LoginUserVo)httpSession.getAttribute(SaapConstants.SESSION_USER);
           if(currentLoginVo==null){
               sessionManager.removeSession(sessionId, loginDevice);
           }else{
               sessionManager.cacheSession(currentLoginVo, request);
           }
       }else{
           HttpSession httpSession=this.getCurrentSession();
           LoginUserVo currentLoginVo=(LoginUserVo)httpSession.getAttribute(SaapConstants.SESSION_USER);
           if(currentLoginVo!=null){
                sessionId=UUID.randomUUID().toString();
                sessionManager.cacheSession(currentLoginVo, request);
           }
       }
       return sessionId;
    }*/
    
    public void setCurrentSession(HttpSession session){
       this.setAttribute(SESSION_ATTR, session); 
    }
    
    public HttpSession getCurrentSession(){
       HttpSession session= (HttpSession)this.getAttribute(SESSION_ATTR);
       if(session==null){
           return super.getSession();
       }else{
           return session;
       }
    }


}
